import {Component, ViewChild} from '@angular/core';
import {
  IonicPage,
  NavController,
  NavParams,
  ViewController,
  AlertController,
  ModalController,
  Events, Platform
} from 'ionic-angular';
import {
  Storage
} from '@ionic/storage'
import {
  Http,
  Headers,
  RequestOptions,
  URLSearchParams
} from '@angular/http';
import {AppConfig} from '../../app/app.config';
import {TabsPage} from '../tabs/tabs';
import {InformationOfOthersPage} from '../information-of-others/information-of-others'
import {baseCommon} from '../../common/baseCommon'

/**
 * Generated class for the GetGroupUsersPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-get-group-users',
  templateUrl: 'get-group-users.html',
})
export class GetGroupUsersPage {

  groupData: any;
  groupUser: any;
  groupLength: any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public http: Http,
              public storage: Storage,
              public alertCtrl: AlertController,
              public modalCtrl: ModalController,
              public event: Events,
              public viewCtrl: ViewController,
              public platform: Platform) {
    this.groupData = this.navParams.data;
  }

  ionViewDidEnter() {

    this.storage.get('sessionID').then(val => {
      baseCommon.getGroupAllUser(this.http , this.groupData.groupID , val)
      .then(data=>{
        this.groupUser = data;
        this.groupLength = this.groupUser.length;
      });
    })
    AppConfig.viewCtrl = this.viewCtrl;
  }


  dismiss() {
    this.viewCtrl.dismiss()
  }

  /**
   * 进入到他人信息页面
   *
   * @param {any} phone
   * @memberof GetGroupUsersPage
   */
  viewOtherData(phone) {

    let model = this.modalCtrl.create(InformationOfOthersPage, {'phone': phone});
    model.dismiss(data => {
      AppConfig.viewCtrl = this.viewCtrl
    })
    model.present();
  }

  /**
   * 退出圈子
   *
   * @param {any} apiUrl
   * @memberof GetGroupUsersPage
   */
  quitThisGroup(apiUrl, id, sessionID) {
    let headers = new Headers({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    let options = new RequestOptions({
      headers: headers,
    });

    let pramas: URLSearchParams = new URLSearchParams();

    pramas.set('id', id);
    pramas.set('sessionID', sessionID)


    this.http.post(apiUrl, pramas, options)
      .toPromise()
      .then(data => {
        let model = this.modalCtrl.create(TabsPage);
        model.present();
      }, err => {
        console.log(err);
      })
  }

  /**
   * 确认退出圈子弹窗
   *
   * @memberof GetGroupUsersPage
   */
  quitGroup() {
    let prompt = this.alertCtrl.create({
      title: '确定要退出此圈子吗？',
      buttons: [{
        text: '我再想想',
        handler: data => {

        }
      },
        {
          text: '确认退出',
          handler: data => {
            this.storage.get('sessionID').then(val => {
              this.quitThisGroup(AppConfig.httpLoc + '/group/exit', this.groupData.groupID, val);
            })
          }
        }
      ]
    });
    prompt.present();
  }

}
